package dolf.objects.weapons.DriveByRocket;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.List;

import dolf.game.*;
import dolf.objects.*;
import dolf.objects.weapons.*;
public class Rocket extends CircleObject implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
//	private int timer; 
	private Player pl;
	private AimObject aim;
	private final double SPEED = 7;
	private Animation explosion; 
	private java.util.LinkedList<String> imgVec = new java.util.LinkedList<String>();

	private AffineTransform affineTransform;
	private BufferedImage image;
	transient private Image img;
	private double oldAngle = 0;

		
	public Rocket(Point pos, Vector motion, AimObject _aim, Player _pl){
		super(pos,5,true,true);
		if(motion!=null){
			this.setMotion(motion);
		}
		this.pl = _pl;
	//	this.timer = 10;
		this.aim = _aim;
		//this.speed = this.getMotion().getLength();
		System.out.println(this.SPEED+" speed");
		img = ImageStore.getInstance().getImage("dolf/objects/weapons/DriveByRocket/gfx/rocket.png");
		for (int i = 1; i <= 23; i++) {
			imgVec.add("dolf/gfx/environment/explosion/explosion"+i+".png");
		}
		explosion = new Animation(imgVec);
	}
	public void move(List<WorldObject> objects, double usedMotion, WorldObject lastKoll){
		double rest = 0;
		WorldObject koll = null;
		/* Wenn Bewegung zu gering => keine Bewegung*/
		/* Kollisions abfrage*/
		double tmprest; 
		rest = this.getRadius()+this.SPEED+1; // sicher groesser als jeder Kollisionswert
		for(WorldObject obj:objects){
			if(obj.isVisible() && obj != this && !(obj instanceof Weapon) && !(obj.isFlat())){
				if((tmprest = this.kollision(obj))>=0){
					if(!(tmprest == rest) || obj instanceof CircleObject){
						if(tmprest < rest ){
							rest = tmprest;
							koll = obj;
						}
					} 
				}
			}	
		}

		if(koll==null){
			/* bewegung ohne kollision*/
			this.moveNormal();
			//this.setPosition(this.getPosition().add(this.getMotion()));
			return;
		} else{
			/*bewgenung mit kollision*/
			this.newMotion(koll);
			return;
		} 
	}
	public void paint(Graphics2D g){
		double angle = Math.atan2(this.getMotion().getY(), this.getMotion().getX());
		Graphics2D g2;
		if(angle != this.oldAngle){
			int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
			affineTransform = AffineTransform.getRotateInstance(angle,this.getRadius(), this.getRadius());
			image = new BufferedImage (2*this.getRadius(), 2*this.getRadius(),mask);
			g2 = (Graphics2D) image.getGraphics();
			g2.drawImage(img, affineTransform, null);
	        			this.oldAngle = angle;
		} else {
			if(image == null){
				int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
				image = new BufferedImage (2*this.getRadius(), 2*this.getRadius(),mask);
			}
		}
       		    
		g.drawImage(image, this.getPosition().getIntX()-this.getRadius(), this.getPosition().getIntY()-this.getRadius(), null);
	}
	public boolean newMotion(WorldObject object){
		
		if(object.isFlat()){
			this.moveNormal();
			return true;
		}
		
		boolean ret = false;

		for(int i = 0; i < Level.getInstance().getPlayers().size(); i++){
			
			Ball obj = (Ball) Level.getInstance().getPlayers().get(i).getBall();
			double dist;
			if(obj.isVisible() && (dist = obj.getPosition().getDist(this.getPosition())) < 25){
					int hit = 40-(int)dist;
					if(obj.getPlayer().getHP()>hit && object == obj){
						ret = true;
					}
					obj.getPlayer().setHP(obj.getPlayer().getHP()- hit);
					pl.addDamage(hit);
					obj.setMotion(obj.getPosition().sub(this.getPosition()).setLength(2));
				}
		}
		
		Level.getInstance().removeObject(this);
		Level.getInstance().removeObject(this.aim);
		this.explosion.startAt(new Point(this.getPosition().getIntX()-27, this.getPosition().getIntY()-60), false);
		
		return ret;
	}
	
	private void moveNormal(){
		this.setMotion(this.getMotion().sub(this.getPosition().sub(this.aim.getPosition()).setLength(0.5)).setLength(this.SPEED));
		this.setPosition(new Point(this.getPosition().getX()+this.getMotion().getX(),this.getPosition().getY()+this.getMotion().getY()));

	}
	public void setAim(AimObject _aim){
		this.aim = _aim;
	}
	public void setPlayer(Player _pl){
		this.pl = _pl;
	}
	public void setMotion(Vector _motion){
		super.setMotion(_motion.setLength(this.SPEED));
	}
	
}
